﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.ComplexClasses;
using BLL;
using ithub.Models;
namespace ithub.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            User objUser = new User();
            return PartialView("Login", objUser);
        }
        [HttpPost]
        public ActionResult Login(User model, string returnUrl)
        {
            User objUser = null;
            objUser = BLL.BL_Users.GetUserbyLoginId(model.login_id);

            if (objUser != null)
            {
                if (objUser.password == model.password)
                {
                    BindSession(model.login_id);
                    return RedirectToAction("Index", "Dashboard");
                }
                else
                {
                    ViewBag.Message = "Please Enter Valid User Name or Password";
                    return View("Login");
                }
            }
            else
            {
                ViewBag.Message = "Please Enter Valid User Name or Password";
                return View("Login");
            }
        }

        public void BindSession( string login_id)
        {
            User objUser  = BLL.BL_Users.GetUserbyLoginId(login_id);
            LoginUser CurrentUser = new LoginUser();
            CurrentUser.myObject = objUser;
            CurrentUser.myClient = BL_Client.GetClientInfo(objUser.client_id);
            CurrentUser.myImage = BL_Images.GetImagebyIDAndModule(objUser.user_id, App_Utils.clsConstants.StringValue.GetStringValue(App_Utils.clsConstants.module.User), true);
            CurrentUser.myAddress = BL_Address.GetAddressInfo(objUser.user_id, App_Utils.clsConstants.StringValue.GetStringValue(App_Utils.clsConstants.module.User));
            CurrentUser.myAdditionalInfo = BL_AdditionalInfo.GetAdditionalInfos(objUser.user_id, App_Utils.clsConstants.StringValue.GetStringValue(App_Utils.clsConstants.module.User));
            CurrentUser.myZone = BL_Zone.GetZonebyZoneId(CurrentUser.myClient.zone_id);

            CurrentUser.myPermissions = BL_Permission.GetPermission(objUser.client_id, objUser.user_id);

            Session[App_Utils.clsConstants.SESSION_USER_ALL] = CurrentUser;
            Session[App_Utils.clsConstants.SESSION_USER] = objUser;
        }
        
        public ActionResult Registration()
        {
            RegistrationModel model = new RegistrationModel();

           return PartialView("Registration", model);
        }

        [HttpPost]
        public ActionResult Registration(RegistrationModel model)
        {
            model.objClient.create_date = DateTime.Now;
            Client objClient = BL_Client.SaveRecord(model.objClient);
            model.objUser.client_id = objClient.client_id;
            model.objUser.create_date = objClient.create_date;
            User objUser = BL_Users.SaveRecord(model.objUser);
            BL_Permission.SaveRecord(new Permission()
            {
                client_id = objUser.client_id,
                user_id = objUser.user_id,
                external_chat = true,
                internal_chat = true,
                is_superadmin = true,
                is_admin = true
            });

            return Login(objUser, "");
        }


        public ActionResult Logout()
        {
            Session.Abandon();
            Session.Clear();
            User objUser = new User();
            return PartialView("Login", objUser);
        }
    }
}
